//
//  ByViewOptionalSubClassScroll.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/15.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewOptionalSubClassScroll.h"
@implementation ByCellOptionalSubClassItem
{
    UILabel     *_titleLabel;
    UILabel     *_priceLabel;
    UITextField *_countTf;
    UIButton    *_countDeleteBtn;
    UIButton    *_countAddBtn;
}

- (instancetype)init{
    
    if (self = [super init]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.textLabel.font = FONT(14);
    }
    return self;
}
- (void)setupUI {
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    _titleLabel = [[UILabel alloc] init];
    [self addSubview:_titleLabel];
    _titleLabel.font = FONT(13);
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(15));
        make.top.equalTo(@(10));
        make.right.equalTo(@(-10));
        make.height.equalTo(@(15));
    }];
    
    _countTf = [[UITextField alloc] init];
    [self addSubview:_countTf];
    CGFloat width = [@"神经科科科科" getSizeOfString:14].width + 40;
    [_countTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-10));
        make.top.equalTo(_titleLabel.mas_bottom);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - width) / 2, 25));
    }];
    _countTf.text = @"1";
    _countTf.font = FONT(14);
    _countTf.textAlignment = NSTextAlignmentCenter;
    
    _countDeleteBtn = [[UIButton alloc] init];
    _countTf.leftView = _countDeleteBtn;
    _countTf.leftViewMode = UITextFieldViewModeAlways;
    [_countDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [_countDeleteBtn setTitle:@"-" forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    _countDeleteBtn.titleLabel.font = FONT(14);
    _countDeleteBtn.layer.cornerRadius = 25 / 2;
    _countDeleteBtn.layer.borderColor = BY_THEME_COLOR.CGColor;
    _countDeleteBtn.layer.borderWidth = 1;
    
    _countAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countTf.rightView = _countAddBtn;
    _countTf.rightViewMode = UITextFieldViewModeAlways;
    [_countAddBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [_countAddBtn setTitle:@"+" forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _countAddBtn.titleLabel.font = FONT(14);
    [_countAddBtn setBackgroundColor:BY_THEME_COLOR];
    _countAddBtn.layer.cornerRadius = 25 / 2;
    
    _priceLabel = [[UILabel alloc] init];
    [self addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(15));
        make.top.equalTo(_titleLabel.mas_bottom).offset(5);
        make.right.equalTo(_countTf.mas_left);
        make.height.equalTo(@(25));
    }];
    
    // 测试数据
    _titleLabel.text = _itemModel.title;
    _priceLabel.text = _itemModel.price;
    
}
- (void)setItemModel:(ByModelPhysicalOptional *)itemModel{
    _itemModel = itemModel;
    [self setupUI];
    
}

@end
#pragma mark - 主视图
@implementation ByViewOptionalSubClassScroll
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    if (self = [super initWithFrame:frame style:style]) {
        
        [self setupDefault];
    }
    return self;
}
// 创建表视图
- (void)setupDefault {
    
    self.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
    self.separatorColor = [UIColor colorWithWhite:0.90 alpha:1];
//    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.itemModels.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellOptionalSubClassItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell = [[ByCellOptionalSubClassItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    ByModelPhysicalOptional *model = self.itemModels[indexPath.row];
    cell.itemModel = model;
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

@end
